﻿using System;
using System.Data;
using System.IO;

namespace YG.CommonFuncs
{
    /// <summary>
    /// data转换成txt
    /// </summary>
    public static class Data2TXT
    {
        public static string GetColumnsByDataTable(DataTable dt)
        {
            string strColumns = null;

            if (dt.Columns.Count > 0)
            {
                //int columnNum = 0;
                //columnNum = dt.Columns.Count;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (i == 0)
                    {
                        strColumns = dt.Columns[i].ColumnName;
                    }
                    else
                    {
                        strColumns += "," + dt.Columns[i].ColumnName;
                    }
                }
            }

            return strColumns;
        }

        public static bool DataTableToTXT(DataTable dt, string strFilePath)
        {
            try
            {
                string columname = GetColumnsByDataTable(dt);

                string strBufferLine = "";
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
                //strmWriterObj.WriteLine(tableheader);
                strmWriterObj.WriteLine(columname);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strBufferLine = "";
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j > 0)
                            strBufferLine += ",";
                        strBufferLine += dt.Rows[i][j].ToString();
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string DataSetToTXT(DataSet dS, string strFilePath, int index)
        {
            try
            {
                string columname = GetColumnsByDataTable(dS.Tables[index]);

                string strBufferLine = "";
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);

                //StreamWriter strmWriterObj = new StreamWriter(strFilePath);
                //strmWriterObj.WriteLine(tableheader);
                strmWriterObj.WriteLine(columname);
                for (int i = 0; i < dS.Tables[index].Rows.Count; i++)
                {
                    strBufferLine = "";
                    for (int j = 0; j < dS.Tables[index].Columns.Count; j++)
                    {
                        if (j > 0)
                            strBufferLine += ",";
                        strBufferLine += dS.Tables[index].Rows[i][j].ToString();
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                return strFilePath;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static string DataSetToTXT(DataSet dS, string strFilePath, string TableName)
        {
            try
            {
                string columname = GetColumnsByDataTable(dS.Tables[TableName]);

                string strBufferLine = "";
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
                strmWriterObj.WriteLine(columname);
                for (int i = 0; i < dS.Tables[TableName].Rows.Count; i++)
                {
                    strBufferLine = "";
                    for (int j = 0; j < dS.Tables[TableName].Columns.Count; j++)
                    {
                        if (j > 0)
                            strBufferLine += ",";
                        strBufferLine += dS.Tables[TableName].Rows[i][j].ToString();
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                return strFilePath;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}